<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>寒冷如铁</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/blog/favicon.ico">
    <script language="javascript" type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="/blog/js/MouseClickEffect.js"></script>
    <meta name="description" content="好奇就尝试，喜欢就坚持！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.01ca31a9.css" as="style"><link rel="preload" href="/blog/assets/js/app.0c46c7d8.js" as="script"><link rel="preload" href="/blog/assets/js/3.5679fe00.js" as="script"><link rel="preload" href="/blog/assets/js/1.8cdd2163.js" as="script"><link rel="preload" href="/blog/assets/js/19.e0e5e91a.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.0b6ffd8d.js"><link rel="prefetch" href="/blog/assets/js/11.d27e4c34.js"><link rel="prefetch" href="/blog/assets/js/12.ba5bd991.js"><link rel="prefetch" href="/blog/assets/js/13.ab5bead7.js"><link rel="prefetch" href="/blog/assets/js/14.124a0dc3.js"><link rel="prefetch" href="/blog/assets/js/15.ac521310.js"><link rel="prefetch" href="/blog/assets/js/16.b32290db.js"><link rel="prefetch" href="/blog/assets/js/17.d65c6ccd.js"><link rel="prefetch" href="/blog/assets/js/18.0a7a10ef.js"><link rel="prefetch" href="/blog/assets/js/20.bcc0e382.js"><link rel="prefetch" href="/blog/assets/js/21.59edea58.js"><link rel="prefetch" href="/blog/assets/js/22.8239cecf.js"><link rel="prefetch" href="/blog/assets/js/23.48516c82.js"><link rel="prefetch" href="/blog/assets/js/24.4e1a046f.js"><link rel="prefetch" href="/blog/assets/js/25.56c0a228.js"><link rel="prefetch" href="/blog/assets/js/26.fb764e02.js"><link rel="prefetch" href="/blog/assets/js/27.f96c7435.js"><link rel="prefetch" href="/blog/assets/js/28.31139fa0.js"><link rel="prefetch" href="/blog/assets/js/29.fa31f05f.js"><link rel="prefetch" href="/blog/assets/js/30.49c791b1.js"><link rel="prefetch" href="/blog/assets/js/31.68660a8e.js"><link rel="prefetch" href="/blog/assets/js/32.4e07e3b0.js"><link rel="prefetch" href="/blog/assets/js/33.e08324f8.js"><link rel="prefetch" href="/blog/assets/js/34.9153a266.js"><link rel="prefetch" href="/blog/assets/js/35.49d3bb37.js"><link rel="prefetch" href="/blog/assets/js/36.5a46a47a.js"><link rel="prefetch" href="/blog/assets/js/37.2403de70.js"><link rel="prefetch" href="/blog/assets/js/38.794a3bde.js"><link rel="prefetch" href="/blog/assets/js/39.185570eb.js"><link rel="prefetch" href="/blog/assets/js/4.a5921925.js"><link rel="prefetch" href="/blog/assets/js/40.b5ddc5b2.js"><link rel="prefetch" href="/blog/assets/js/41.81f131ab.js"><link rel="prefetch" href="/blog/assets/js/42.474cb45b.js"><link rel="prefetch" href="/blog/assets/js/43.fe269b94.js"><link rel="prefetch" href="/blog/assets/js/44.123909bf.js"><link rel="prefetch" href="/blog/assets/js/45.85e17df3.js"><link rel="prefetch" href="/blog/assets/js/46.96eb5367.js"><link rel="prefetch" href="/blog/assets/js/47.2c06ceb7.js"><link rel="prefetch" href="/blog/assets/js/48.dcaa5d75.js"><link rel="prefetch" href="/blog/assets/js/49.47475370.js"><link rel="prefetch" href="/blog/assets/js/5.a6bfa893.js"><link rel="prefetch" href="/blog/assets/js/50.29aa41a9.js"><link rel="prefetch" href="/blog/assets/js/51.b049cb19.js"><link rel="prefetch" href="/blog/assets/js/52.9f16a8d1.js"><link rel="prefetch" href="/blog/assets/js/53.9ccedc0d.js"><link rel="prefetch" href="/blog/assets/js/54.c16fde39.js"><link rel="prefetch" href="/blog/assets/js/55.3d2e78cc.js"><link rel="prefetch" href="/blog/assets/js/56.14b1dddb.js"><link rel="prefetch" href="/blog/assets/js/57.a48eb0da.js"><link rel="prefetch" href="/blog/assets/js/58.db985774.js"><link rel="prefetch" href="/blog/assets/js/59.dbb67461.js"><link rel="prefetch" href="/blog/assets/js/6.d5f7873e.js"><link rel="prefetch" href="/blog/assets/js/60.fd9edd5c.js"><link rel="prefetch" href="/blog/assets/js/61.c87eaa37.js"><link rel="prefetch" href="/blog/assets/js/62.09bd4303.js"><link rel="prefetch" href="/blog/assets/js/63.c3a4cb82.js"><link rel="prefetch" href="/blog/assets/js/64.1e623e6b.js"><link rel="prefetch" href="/blog/assets/js/65.5ee98079.js"><link rel="prefetch" href="/blog/assets/js/66.875fdeb3.js"><link rel="prefetch" href="/blog/assets/js/67.52e4feee.js"><link rel="prefetch" href="/blog/assets/js/68.09bb6522.js"><link rel="prefetch" href="/blog/assets/js/69.e5cc1032.js"><link rel="prefetch" href="/blog/assets/js/7.63c57787.js"><link rel="prefetch" href="/blog/assets/js/70.a2307508.js"><link rel="prefetch" href="/blog/assets/js/71.75bc515a.js"><link rel="prefetch" href="/blog/assets/js/72.f7d10a87.js"><link rel="prefetch" href="/blog/assets/js/73.ae777bb5.js"><link rel="prefetch" href="/blog/assets/js/74.ddd90b9a.js"><link rel="prefetch" href="/blog/assets/js/75.5d297adc.js"><link rel="prefetch" href="/blog/assets/js/76.299aaa21.js"><link rel="prefetch" href="/blog/assets/js/77.c93068f7.js"><link rel="prefetch" href="/blog/assets/js/78.727916c9.js"><link rel="prefetch" href="/blog/assets/js/79.605f2aff.js"><link rel="prefetch" href="/blog/assets/js/8.22284503.js"><link rel="prefetch" href="/blog/assets/js/80.0081929d.js"><link rel="prefetch" href="/blog/assets/js/9.8e870e00.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.01ca31a9.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-19557b78><div data-v-19557b78><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-19557b78 data-v-19557b78><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-19557b78 data-v-19557b78><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>寒冷如铁</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>寒铁</span>
            
          <span data-v-64685f0e>2020 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-19557b78><header class="navbar" data-v-19557b78><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">寒冷如铁</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/build/" class="nav-link"><i class="iconfont undefined"></i>
  build
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/db/" class="nav-link"><i class="iconfont undefined"></i>
  db
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/docker/" class="nav-link"><i class="iconfont undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/workflow/" class="nav-link"><i class="iconfont undefined"></i>
  workflow
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/git/" class="nav-link"><i class="iconfont undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/java/" class="nav-link"><i class="iconfont undefined"></i>
  java
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/source/" class="nav-link"><i class="iconfont undefined"></i>
  source
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/linux/" class="nav-link"><i class="iconfont undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/network/" class="nav-link"><i class="iconfont undefined"></i>
  network
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/project/" class="nav-link"><i class="iconfont undefined"></i>
  project
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/share/" class="nav-link"><i class="iconfont undefined"></i>
  share
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/software/" class="nav-link"><i class="iconfont undefined"></i>
  software
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/spring/" class="nav-link"><i class="iconfont undefined"></i>
  spring
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/tomcat/" class="nav-link"><i class="iconfont undefined"></i>
  tomcat
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/web/" class="nav-link"><i class="iconfont undefined"></i>
  web
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/views/guide.html" class="nav-link"><i class="iconfont reco-other"></i>
  记录
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      外链
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/huhuhan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/huhu_han" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  有道云笔记
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  LeadCloud
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://cloud.seafile.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Seafile
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://huhu_han.gitee.io/downgit" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  DownGit
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-19557b78></div> <aside class="sidebar" data-v-19557b78><div class="personal-info-wrapper" data-v-b038cec6><img src="/blog/avatar.png" alt="author-avatar" class="personal-img" data-v-b038cec6> <h3 class="name" data-v-b038cec6>
    寒铁
  </h3> <div class="num" data-v-b038cec6><div data-v-b038cec6><h3 data-v-b038cec6>66</h3> <h6 data-v-b038cec6>文章</h6></div> <div data-v-b038cec6><h3 data-v-b038cec6>17</h3> <h6 data-v-b038cec6>标签</h6></div></div> <hr data-v-b038cec6></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/build/" class="nav-link"><i class="iconfont undefined"></i>
  build
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/db/" class="nav-link"><i class="iconfont undefined"></i>
  db
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/docker/" class="nav-link"><i class="iconfont undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/workflow/" class="nav-link"><i class="iconfont undefined"></i>
  workflow
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/git/" class="nav-link"><i class="iconfont undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/java/" class="nav-link"><i class="iconfont undefined"></i>
  java
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/source/" class="nav-link"><i class="iconfont undefined"></i>
  source
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/linux/" class="nav-link"><i class="iconfont undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/network/" class="nav-link"><i class="iconfont undefined"></i>
  network
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/project/" class="nav-link"><i class="iconfont undefined"></i>
  project
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/share/" class="nav-link"><i class="iconfont undefined"></i>
  share
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/software/" class="nav-link"><i class="iconfont undefined"></i>
  software
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/spring/" class="nav-link"><i class="iconfont undefined"></i>
  spring
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/tomcat/" class="nav-link"><i class="iconfont undefined"></i>
  tomcat
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/web/" class="nav-link"><i class="iconfont undefined"></i>
  web
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/views/guide.html" class="nav-link"><i class="iconfont reco-other"></i>
  记录
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      外链
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/huhuhan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/huhu_han" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  有道云笔记
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  LeadCloud
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://cloud.seafile.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Seafile
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://huhu_han.gitee.io/downgit" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  DownGit
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span></span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/views/db/redis.html#安装" class="sidebar-link">安装</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#服务端" class="sidebar-link">服务端</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#客户端" class="sidebar-link">客户端</a></li></ul></li><li><a href="/blog/views/db/redis.html#spring-boot-应用" class="sidebar-link">Spring Boot 应用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#背景简介" class="sidebar-link">背景简介</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#引入配置" class="sidebar-link">引入配置</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#源码简析" class="sidebar-link">源码简析</a></li></ul></li><li><a href="/blog/views/db/redis.html#spring-boot-例子" class="sidebar-link">Spring Boot 例子</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#字符串" class="sidebar-link">字符串</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#哈希表" class="sidebar-link">哈希表</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#列表" class="sidebar-link">列表</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#集合" class="sidebar-link">集合</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#有序集合" class="sidebar-link">有序集合</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#hyperloglog" class="sidebar-link">HyperLogLog</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#位图" class="sidebar-link">位图</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#地理位置" class="sidebar-link">地理位置</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#数据库" class="sidebar-link">数据库</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#自动过期" class="sidebar-link">自动过期</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#事务" class="sidebar-link">事务</a></li></ul></li><li><a href="/blog/views/db/redis.html#现象-策略" class="sidebar-link">现象&amp;策略</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#缓存过期-缓存淘汰" class="sidebar-link">缓存过期 &amp; 缓存淘汰</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#缓存穿透-布隆过滤器" class="sidebar-link">缓存穿透 &amp; 布隆过滤器</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#缓存击穿-缓存雪崩" class="sidebar-link">缓存击穿 &amp; 缓存雪崩</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#进程崩溃-持久化" class="sidebar-link">进程崩溃 &amp; 持久化</a></li></ul></li><li><a href="/blog/views/db/redis.html#集群" class="sidebar-link">集群</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#docker搭建" class="sidebar-link">Docker搭建</a></li><li class="sidebar-sub-header"><a href="/blog/views/db/redis.html#集群操作" class="sidebar-link">集群操作</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-19557b78><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e></h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>寒铁</span>
            
          <span data-v-64685f0e>2020 - </span>
          2022
        </a></span></div></div> <div data-v-19557b78><main class="page"><div class="page-title" style="display:none;"><h1></h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>寒铁</span></i> <!----> <i class="iconfont reco-eye" data-v-484a899e><span id="/blog/views/db/redis.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-484a899e><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <!----></div></div> <div class="theme-reco-content content__default" style="display:none;"><p>npm---
title: Redis 相关总结
date: 2020-08-26
tags:</p> <ul><li>db
categories:</li> <li>db</li></ul> <hr> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201012105814.png" alt=""></p> <h2 id="安装"><a href="#安装" class="header-anchor">#</a> 安装</h2> <h3 id="服务端"><a href="#服务端" class="header-anchor">#</a> 服务端</h3> <ul><li><p>window，压缩包，运行redis-server.exe</p></li> <li><p>docker 部署</p> <p>配置参考：docker-compose</p> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">'3'</span>

<span class="token key atrule">services</span><span class="token punctuation">:</span>
  <span class="token key atrule">redis2</span><span class="token punctuation">:</span>
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis2
    <span class="token key atrule">restart</span><span class="token punctuation">:</span> always
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis<span class="token punctuation">:</span>latest
    <span class="token comment"># 覆盖内部默认命令，指定自定义配置文件启动</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span>
      /bin/bash <span class="token punctuation">-</span>c &quot;redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf&quot;  
    <span class="token key atrule">environment</span><span class="token punctuation">:</span>
      <span class="token key atrule">TZ</span><span class="token punctuation">:</span> Asia/Shanghai
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7379<span class="token punctuation">:</span><span class="token number">6379</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
    <span class="token comment"># rw指读写权限</span>
      <span class="token punctuation">-</span> ./config/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf<span class="token punctuation">:</span>rw
      <span class="token punctuation">-</span> ./data<span class="token punctuation">:</span>/data<span class="token punctuation">:</span>rw
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>配置参考：redis.conf</p> <div class="language-ini line-numbers-mode"><pre class="language-ini"><code><span class="token comment"># 监听地址, 本机网卡IP，通过ifconfig查看lo网卡</span>
<span class="token comment"># 127.0.0.1 仅本机访问</span>
<span class="token comment">#bind 127.0.0.1</span>
<span class="token comment"># 默认为空或填0.0.0.0，任意访问</span>
<span class="token comment">#bind 0.0.0.0</span>
<span class="token comment"># 保护模式，开启后只有本机可以连接访问</span>
protected-mode no
<span class="token comment"># 监听端口</span>
port 6379
<span class="token comment"># 连接密码</span>
requirepass huhuhan
<span class="token comment"># db库数量</span>
databases 16
<span class="token comment"># 超时时间，默认不超时</span>
timeout 0
<span class="token comment"># 挂载目录</span>
dir /data
<span class="token comment"># 最大连接客户端</span>
<span class="token comment">#maxclients 10000  </span>

<span class="token comment"># 持久化-RDB模式的文件名</span>
dbfilename dump.rdb
<span class="token comment"># RDB模式同步，默认3种配置</span>
<span class="token comment"># 如900秒后，有一个键发生改变</span>
save 900 1
save 300 10
save 60 10000
<span class="token comment"># 禁用文件校验，减少持久化时资源消耗</span>
rdbcompression yes

<span class="token comment"># 是否开启AOF模式，可以开启2个模式，AOF为主，每次启动服务执行RDB</span>
appendonly no
<span class="token comment"># 持久化-AOF模式的文件名</span>
<span class="token comment">#appendfilename &quot;appendonly.aof&quot;</span>
<span class="token comment"># AOF模式同步，默认每秒，|no 不执行|always 每次写入执行</span>
appendfsync everysec
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div></li></ul> <h3 id="客户端"><a href="#客户端" class="header-anchor">#</a> 客户端</h3> <ul><li><p>自带的redis-cli.exe</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>127.0.0.1:6379&gt; config get requirepass
1) &quot;requirepass&quot;
2) &quot;&quot;
127.0.0.1:6379&gt; config set requirepass 123456
OK
127.0.0.1:6379&gt; config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379&gt; auth 123456
OK
127.0.0.1:6379&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li> <li><p><a href="https://github.com/uglide/RedisDesktopManager" target="_blank" rel="noopener noreferrer">Redis Desktop Manager<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></li></ul> <h2 id="spring-boot-应用"><a href="#spring-boot-应用" class="header-anchor">#</a> Spring Boot 应用</h2> <h3 id="背景简介"><a href="#背景简介" class="header-anchor">#</a> 背景简介</h3> <ul><li><strong>Lettuce</strong> 和 <strong>Jedis</strong>的定位都是<strong>Redis</strong>的<strong>客户端</strong></li> <li><strong>Jedis</strong>在实现上是<strong>直接连接Redis服务端</strong>，如果在多线程环境下是非线程安全的，这个时候只有使用连接池，为每个<strong>Jedis</strong>实例增加物理连接</li> <li><strong>Lettuce</strong> 的连接是<strong>基于Netty</strong>，连接实例（<code>StatefulRedisConnection</code>线程安全）可以在<strong>多个线程间并发访问</strong>，因此一个连接实例就可以满足多线程环境下的并发访问，一个不够的情况也可以按需增加连接实例</li> <li>2.x之前Redis的连接池为<strong>Jedis</strong>，2.x之后改为了<strong>Lettuce</strong> ，<strong>Lettuce</strong> 能够支持<strong>Redis4</strong>(需要JAVA8及以上）</li></ul> <h3 id="引入配置"><a href="#引入配置" class="header-anchor">#</a> 引入配置</h3> <ul><li><p>依赖包</p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-data-redis<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>开启注解<code>@EnableCaching</code></p></li> <li><p>application属性配置</p> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
    <span class="token key atrule">redis</span><span class="token punctuation">:</span>
      <span class="token key atrule">host</span><span class="token punctuation">:</span> localhost
      <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">6379</span>
      <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token number">123456</span>
      <span class="token key atrule">timeout</span><span class="token punctuation">:</span> <span class="token number">2000</span>
      <span class="token key atrule">database</span><span class="token punctuation">:</span> <span class="token number">2</span>
      <span class="token comment">#使用lettuce连接池实例连接redis服务器(2.0版本前用jedis)</span>
      <span class="token key atrule">lettuce</span><span class="token punctuation">:</span>
        <span class="token key atrule">pool</span><span class="token punctuation">:</span>
          <span class="token key atrule">max-active</span><span class="token punctuation">:</span> <span class="token number">8</span>
          <span class="token key atrule">max-wait</span><span class="token punctuation">:</span> <span class="token number">1</span>
          <span class="token key atrule">max-idle</span><span class="token punctuation">:</span> <span class="token number">8</span>
          <span class="token key atrule">min-idle</span><span class="token punctuation">:</span> <span class="token number">0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></li> <li><p>模板配置</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>fasterxml<span class="token punctuation">.</span>jackson<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">JsonAutoDetect</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>fasterxml<span class="token punctuation">.</span>jackson<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">PropertyAccessor</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>fasterxml<span class="token punctuation">.</span>jackson<span class="token punctuation">.</span>databind<span class="token punctuation">.</span></span><span class="token class-name">ObjectMapper</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cache<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">CachingConfigurerSupport</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Bean</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Configuration</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>redis<span class="token punctuation">.</span>connection<span class="token punctuation">.</span></span><span class="token class-name">RedisConnectionFactory</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>redis<span class="token punctuation">.</span>core<span class="token punctuation">.</span></span><span class="token class-name">RedisTemplate</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>redis<span class="token punctuation">.</span>serializer<span class="token punctuation">.</span></span><span class="token class-name">Jackson2JsonRedisSerializer</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>redis<span class="token punctuation">.</span>serializer<span class="token punctuation">.</span></span><span class="token class-name">StringRedisSerializer</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * @author yanghan
 * @date 2019/7/1
 */</span>
<span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RedisConfig</span> <span class="token keyword">extends</span> <span class="token class-name">CachingConfigurerSupport</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * RedisTemplate配置
     */</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">RedisTemplate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> <span class="token function">redisTemplate</span><span class="token punctuation">(</span><span class="token class-name">RedisConnectionFactory</span> factory<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">RedisTemplate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> template <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RedisTemplate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        template<span class="token punctuation">.</span><span class="token function">setConnectionFactory</span><span class="token punctuation">(</span>factory<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Jackson2JsonRedisSerializer</span> jackson2JsonRedisSerializer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Jackson2JsonRedisSerializer</span><span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">ObjectMapper</span> om <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectMapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        om<span class="token punctuation">.</span><span class="token function">setVisibility</span><span class="token punctuation">(</span><span class="token class-name">PropertyAccessor</span><span class="token punctuation">.</span>ALL<span class="token punctuation">,</span> <span class="token class-name">JsonAutoDetect<span class="token punctuation">.</span>Visibility</span><span class="token punctuation">.</span>ANY<span class="token punctuation">)</span><span class="token punctuation">;</span>
        om<span class="token punctuation">.</span><span class="token function">enableDefaultTyping</span><span class="token punctuation">(</span><span class="token class-name">ObjectMapper<span class="token punctuation">.</span>DefaultTyping</span><span class="token punctuation">.</span>NON_FINAL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        jackson2JsonRedisSerializer<span class="token punctuation">.</span><span class="token function">setObjectMapper</span><span class="token punctuation">(</span>om<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">StringRedisSerializer</span> stringRedisSerializer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringRedisSerializer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// key采用String的序列化方式</span>
        template<span class="token punctuation">.</span><span class="token function">setKeySerializer</span><span class="token punctuation">(</span>stringRedisSerializer<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// hash的key也采用String的序列化方式</span>
        template<span class="token punctuation">.</span><span class="token function">setHashKeySerializer</span><span class="token punctuation">(</span>stringRedisSerializer<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// value序列化方式采用jackson</span>
        template<span class="token punctuation">.</span><span class="token function">setValueSerializer</span><span class="token punctuation">(</span>jackson2JsonRedisSerializer<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// hash的value序列化方式采用jackson</span>
        template<span class="token punctuation">.</span><span class="token function">setHashValueSerializer</span><span class="token punctuation">(</span>jackson2JsonRedisSerializer<span class="token punctuation">)</span><span class="token punctuation">;</span>
        template<span class="token punctuation">.</span><span class="token function">afterPropertiesSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> template<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br></div></div></li> <li><p>应用</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> <span class="token class-name">RedisTemplate</span> redisTemplate<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> redisConnection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
            <span class="token comment">//redisConnection执行操作</span>
            <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></li></ul> <h3 id="源码简析"><a href="#源码简析" class="header-anchor">#</a> 源码简析</h3> <ul><li><p><strong>RedisConnection</strong>是redis的连接集成接口，继承了<strong>RedisCommands</strong>操作接口；</p></li> <li><p>而<strong>RedisCommands</strong>继承所有的Redis操作命令接口</p> <ul><li><p>比如集合类命令（使用IDEA的diagram功能）</p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201029151838.png" alt=""></p></li></ul></li> <li><p><strong>RedisConnection</strong> 子接口和方法</p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201029142002.png" alt=""></p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201029152156.png" alt=""></p></li></ul> <h2 id="spring-boot-例子"><a href="#spring-boot-例子" class="header-anchor">#</a> Spring Boot 例子</h2> <h3 id="字符串"><a href="#字符串" class="header-anchor">#</a> 字符串</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> valueBytes <span class="token operator">=</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">long</span> secondTimeout <span class="token operator">=</span> <span class="token number">100l</span><span class="token punctuation">;</span><span class="token comment">//单位：秒</span>
        <span class="token keyword">long</span> milliSecondTimeout <span class="token operator">=</span> <span class="token number">100l</span><span class="token punctuation">;</span><span class="token comment">//单位：秒</span>
        <span class="token comment">//key、value存储，类似Map</span>
        connection<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> valueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//参数：过期时间、先判断，Expiration，SetOption</span>
        connection<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> valueBytes<span class="token punctuation">,</span> <span class="token class-name">Expiration</span><span class="token punctuation">.</span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">RedisStringCommands<span class="token punctuation">.</span>SetOption</span><span class="token punctuation">.</span>SET_IF_ABSENT<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//参数：过期时间，单位秒，推荐用set方法+参数</span>
        connection<span class="token punctuation">.</span><span class="token function">setEx</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> secondTimeout<span class="token punctuation">,</span> valueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//只在键不存在才执行，等于RedisStringCommands.SetOption.SET_IF_ABSENT</span>
        connection<span class="token punctuation">.</span><span class="token function">setNX</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> valueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//过期时间：毫秒单位</span>
        connection<span class="token punctuation">.</span><span class="token function">pSetEx</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> milliSecondTimeout<span class="token punctuation">,</span> valueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//设置过期时间</span>
        connection<span class="token punctuation">.</span><span class="token function">expire</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> secondTimeout<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">pExpire</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> milliSecondTimeout<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//取值，只限于字符串key的值</span>
        connection<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//先去旧值，并存储新值</span>
        connection<span class="token punctuation">.</span><span class="token function">getSet</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> valueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//批量存取</span>
        <span class="token class-name">Map</span><span class="token operator">&lt;</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">&gt;</span> keyMap <span class="token operator">=</span> <span class="token class-name">Maps</span><span class="token punctuation">.</span><span class="token function">newHashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        keyMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> valueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        keyMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;value2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">mSet</span><span class="token punctuation">(</span>keyMap<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">mSetNX</span><span class="token punctuation">(</span>keyMap<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//当且仅当所有key都不存在时才执行</span>
        connection<span class="token punctuation">.</span><span class="token function">mGet</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//获取key对应的value字符串长度</span>
        connection<span class="token punctuation">.</span><span class="token function">strLen</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//key对应的value结尾追加新值</span>
        connection<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;newValue&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//在value值偏移5后替换为“rangeValue”，相当于substring+replace方法</span>
        connection<span class="token punctuation">.</span><span class="token function">setRange</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;rangeValue&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//取[0,4]区级的字符，类似于substring该方法范围[0,4)</span>
        connection<span class="token punctuation">.</span><span class="token function">getRange</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//递增+1，value仅限于有符号整数，按十进制算法，数字在redis中以字符串显示</span>
        connection<span class="token punctuation">.</span><span class="token function">incr</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">incrBy</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token number">20l</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//加20</span>
        connection<span class="token punctuation">.</span><span class="token function">decr</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//减1</span>
        connection<span class="token punctuation">.</span><span class="token function">decrBy</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//减20</span>

        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br></div></div><h3 id="哈希表"><a href="#哈希表" class="header-anchor">#</a> 哈希表</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> mapKeyBytes <span class="token operator">=</span> <span class="token string">&quot;mapKey&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> mapValueBytes <span class="token operator">=</span> <span class="token string">&quot;mapValue&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">long</span> secondTimeout <span class="token operator">=</span> <span class="token number">100l</span><span class="token punctuation">;</span><span class="token comment">//单位：秒</span>
        <span class="token keyword">long</span> milliSecondTimeout <span class="token operator">=</span> <span class="token number">100l</span><span class="token punctuation">;</span><span class="token comment">//单位：秒</span>

        <span class="token comment">//存取哈希表Map</span>
        connection<span class="token punctuation">.</span><span class="token function">hSet</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyBytes<span class="token punctuation">,</span> mapValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//成功返回1，成功并覆盖操作返回0</span>
        connection<span class="token punctuation">.</span><span class="token function">hGet</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">hSetNX</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyBytes<span class="token punctuation">,</span> mapValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//先判断再操作</span>
        <span class="token comment">//是否存在</span>
        connection<span class="token punctuation">.</span><span class="token function">hExists</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//删除Map中key，支持多个操作</span>
        connection<span class="token punctuation">.</span><span class="token function">hDel</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyBytes<span class="token punctuation">,</span> <span class="token string">&quot;mapKey2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//Map的大小</span>
        connection<span class="token punctuation">.</span><span class="token function">hLen</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//Map中key的值加20</span>
        connection<span class="token punctuation">.</span><span class="token function">hIncrBy</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyBytes<span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//批量存取</span>
        <span class="token class-name">Map</span><span class="token operator">&lt;</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">&gt;</span> mapKeyMap <span class="token operator">=</span> <span class="token class-name">Maps</span><span class="token punctuation">.</span><span class="token function">newHashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        mapKeyMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        mapKeyMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;mapKey2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;mapValue2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">hMSet</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyMap<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">hMGet</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> mapKeyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">hGetAll</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//取整个Map</span>
        <span class="token comment">//类似于Map的keySet，valueSet方法</span>
        connection<span class="token punctuation">.</span><span class="token function">hKeys</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">hVals</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h3 id="列表"><a href="#列表" class="header-anchor">#</a> 列表</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Array</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Array</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arrayValueBytes <span class="token operator">=</span> <span class="token string">&quot;arrayValue&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">long</span> secondTimeout <span class="token operator">=</span> <span class="token number">100l</span><span class="token punctuation">;</span><span class="token comment">//单位：秒</span>
        <span class="token keyword">long</span> milliSecondTimeout <span class="token operator">=</span> <span class="token number">100l</span><span class="token punctuation">;</span><span class="token comment">//单位：秒</span>

        <span class="token comment">//支持push多个</span>
        connection<span class="token punctuation">.</span><span class="token function">lPush</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> arrayValueBytes<span class="token punctuation">,</span> <span class="token string">&quot;arrayValue2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//在左边push，即表头，等同于lPushX（仅一个参数）</span>
        connection<span class="token punctuation">.</span><span class="token function">lPush</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> arrayValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//在右边push，即表尾，等同于rPushX（仅一个参数）</span>
        connection<span class="token punctuation">.</span><span class="token function">rPush</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> arrayValueBytes<span class="token punctuation">,</span> <span class="token string">&quot;arrayValue2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回区级值数量的列表，-1表示最后一个</span>
        connection<span class="token punctuation">.</span><span class="token function">lRange</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//返回3个</span>

        <span class="token comment">//移除并返回</span>
        connection<span class="token punctuation">.</span><span class="token function">lPop</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//表头</span>
        connection<span class="token punctuation">.</span><span class="token function">rPop</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//表尾</span>
        <span class="token comment">//key1的表尾移除加到key2的表头</span>
        connection<span class="token punctuation">.</span><span class="token function">rPopLPush</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//移除value相同值的数据，数量为count,</span>
        <span class="token comment">// count &gt; 0 : 从表头开始向表尾搜索</span>
        <span class="token comment">// count &lt; 0 : 从表尾开始向表头搜索</span>
        <span class="token comment">// count = 0 : 移除表中所有与 value 相等的值</span>
        connection<span class="token punctuation">.</span><span class="token function">lRem</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token number">2</span><span class="token comment">/*count*/</span><span class="token punctuation">,</span> arrayValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">long</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token comment">//获取列表大小</span>
        connection<span class="token punctuation">.</span><span class="token function">lLen</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回指定下标的value</span>
        connection<span class="token punctuation">.</span><span class="token function">lIndex</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> index<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//在value前或后插入某新值</span>
        connection<span class="token punctuation">.</span><span class="token function">lInsert</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">RedisListCommands<span class="token punctuation">.</span>Position</span><span class="token punctuation">.</span>BEFORE<span class="token punctuation">,</span>  arrayValueBytes<span class="token punctuation">,</span> <span class="token string">&quot;insertValue&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//在指定下标位置存储值</span>
        connection<span class="token punctuation">.</span><span class="token function">lSet</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> index<span class="token punctuation">,</span> arrayValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//裁剪列表指定区间的数据</span>
        connection<span class="token punctuation">.</span><span class="token function">lTrim</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


        <span class="token keyword">int</span> timeout <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token comment">//秒，0表示无限阻塞直到执行</span>
        <span class="token comment">//从左/右开始搜索执行存在key的列表，找到就移除返回，否则阻塞至超时,  返回2个元素的列表，一个为key一个为value</span>
        <span class="token class-name">List</span><span class="token operator">&lt;</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">&gt;</span> r1 <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">bLPop</span><span class="token punctuation">(</span>timeout<span class="token punctuation">,</span> keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br></div></div><h3 id="集合"><a href="#集合" class="header-anchor">#</a> 集合</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Collection</span><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Collection</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> listValueBytes <span class="token operator">=</span> <span class="token string">&quot;listValue&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//添加集合元素，支持多个</span>
        connection<span class="token punctuation">.</span><span class="token function">sAdd</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">,</span> <span class="token string">&quot;listValue2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//集合是否包含元素</span>
        connection<span class="token punctuation">.</span><span class="token function">sIsMember</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//移除随机元素</span>
        connection<span class="token punctuation">.</span><span class="token function">sPop</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回随机元素</span>
        connection<span class="token punctuation">.</span><span class="token function">sRandMember</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回随机count个元素，不等于0</span>
        <span class="token keyword">long</span> count <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//&gt;0，不会重复，小于0会有重复元素</span>
        connection<span class="token punctuation">.</span><span class="token function">sRandMember</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//移除指定元素</span>
        connection<span class="token punctuation">.</span><span class="token function">sRem</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">,</span> <span class="token string">&quot;listValue2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//移动元素key到key2</span>
        connection<span class="token punctuation">.</span><span class="token function">sMove</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//统计集合大小</span>
        connection<span class="token punctuation">.</span><span class="token function">sCard</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回所有元素</span>
        connection<span class="token punctuation">.</span><span class="token function">sMembers</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回交集</span>
        connection<span class="token punctuation">.</span><span class="token function">sInter</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;key3&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//交集存储在新key中</span>
        connection<span class="token punctuation">.</span><span class="token function">sInterStore</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;newKey&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//并集</span>
        connection<span class="token punctuation">.</span><span class="token function">sUnion</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;key3&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">sUnionStore</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;newKey&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//差集（属于A集合不属于B集合）</span>
        connection<span class="token punctuation">.</span><span class="token function">sDiff</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;key3&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">sDiffStore</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;newKey&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h3 id="有序集合"><a href="#有序集合" class="header-anchor">#</a> 有序集合</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> listValueBytes <span class="token operator">=</span> <span class="token string">&quot;listValue&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">double</span> score <span class="token operator">=</span> <span class="token number">1.2d</span><span class="token punctuation">;</span>
        <span class="token keyword">double</span> increScore <span class="token operator">=</span> <span class="token number">10d</span><span class="token punctuation">;</span>
        <span class="token keyword">double</span> minScore <span class="token operator">=</span> <span class="token number">1d</span><span class="token punctuation">;</span>
        <span class="token keyword">double</span> maxScore <span class="token operator">=</span> <span class="token number">2d</span><span class="token punctuation">;</span>
        <span class="token keyword">long</span> start <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//下标</span>
        <span class="token keyword">long</span> stop <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token comment">//-1表示最后个成员</span>

        <span class="token comment">//添加数据，一个value包含一个score值，可重写score值</span>
        connection<span class="token punctuation">.</span><span class="token function">zAdd</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> score<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回指定value的score</span>
        connection<span class="token punctuation">.</span><span class="token function">zScore</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//增加对应的score值</span>
        connection<span class="token punctuation">.</span><span class="token function">zIncrBy</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> increScore<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">zCard</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//集合大小</span>
        <span class="token comment">//筛选区间内score值的成员</span>
        connection<span class="token punctuation">.</span><span class="token function">zCount</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> minScore<span class="token punctuation">,</span> maxScore<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//返回区间下标的成员，从小到大排序</span>
        connection<span class="token punctuation">.</span><span class="token function">zRange</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">zRangeWithScores</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//包括score</span>
        connection<span class="token punctuation">.</span><span class="token function">zRangeByScore</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> minScore<span class="token punctuation">,</span> maxScore<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//按score区间[start,stop]返回，</span>
        connection<span class="token punctuation">.</span><span class="token function">zRangeByScoreWithScores</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span>  minScore<span class="token punctuation">,</span> maxScore<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">zRangeByScoreWithScores</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">RedisZSetCommands<span class="token punctuation">.</span>Range</span><span class="token punctuation">.</span><span class="token function">range</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lt</span><span class="token punctuation">(</span>maxScore<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">gt</span><span class="token punctuation">(</span>minScore<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//按rang区间返回</span>
        connection<span class="token punctuation">.</span><span class="token function">zRangeByLex</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//按集合元素区间返回，限制先返回哪些</span>
        connection<span class="token punctuation">.</span><span class="token function">zRangeByLex</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">RedisZSetCommands<span class="token punctuation">.</span>Range</span><span class="token punctuation">.</span><span class="token function">range</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">RedisZSetCommands<span class="token punctuation">.</span>Limit</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回区间下标的成员，从大到小排序</span>
        connection<span class="token punctuation">.</span><span class="token function">zRevRange</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//.. 类似方法同上</span>

        <span class="token comment">//返回指定value的排序，</span>
        connection<span class="token punctuation">.</span><span class="token function">zRank</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//从小到大，排序最小为0</span>
        connection<span class="token punctuation">.</span><span class="token function">zRevRank</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//从大到小，排序最小为0</span>

        <span class="token comment">//移除并返回指定元素，支持多个</span>
        connection<span class="token punctuation">.</span><span class="token function">zRem</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> listValueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">zRemRangeByScore</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> minScore<span class="token punctuation">,</span> maxScore<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//按score区间移除</span>
        connection<span class="token punctuation">.</span><span class="token function">zRemRange</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> start<span class="token punctuation">,</span> stop<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//按下标区间移除</span>

        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br></div></div><h3 id="hyperloglog"><a href="#hyperloglog" class="header-anchor">#</a> HyperLogLog</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> valueBytes <span class="token operator">=</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">/**
         * HyperLogLog数据结构：是用来做基数统计的算法
         * 优点：即使输入元素的数量或者体积非常非常大，计算基数所需的空间总是固定的、并且是很小的。
         * 基数：元素数量（不统计重复的，如[a,b,c,a,b]=3）
         * 估算值：算法给出的基数并不是精确的，可能会比实际稍微多一些或者稍微少一些，但会控制在合理的范围之内。
         * 应用：存储网站访问ip（若用字符串存储集合，耗内存）, 来统计网站的ip访问数量
         */</span>

        <span class="token comment">//存储，支持多个元素</span>
        connection<span class="token punctuation">.</span><span class="token function">pfAdd</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> valueBytes<span class="token punctuation">,</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回Log的基数估算值</span>
        connection<span class="token punctuation">.</span><span class="token function">pfCount</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//合并多个Log，并集后再算出来基数估算值</span>
        connection<span class="token punctuation">.</span><span class="token function">pfMerge</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;key3&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="位图"><a href="#位图" class="header-anchor">#</a> 位图</h3> <p><a href="http://redisdoc.com/bitmap/index.html" target="_blank" rel="noopener noreferrer">官方文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="地理位置"><a href="#地理位置" class="header-anchor">#</a> 地理位置</h3> <p><a href="http://redisdoc.com/geo/index.html" target="_blank" rel="noopener noreferrer">官方文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="数据库"><a href="#数据库" class="header-anchor">#</a> 数据库</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> valueBytes <span class="token operator">=</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> dbIndex <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> pattern <span class="token operator">=</span> <span class="token string">&quot;pattern简单正则匹配&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//是否存在</span>
        connection<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//返回key的数据结构：string、list、set、zset、hash、none（不存在的key）</span>
        connection<span class="token punctuation">.</span><span class="token function">type</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//重名key</span>
        connection<span class="token punctuation">.</span><span class="token function">rename</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;newKey&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">renameNX</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;newKey&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//移动数据库</span>
        connection<span class="token punctuation">.</span><span class="token function">move</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> dbIndex<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//删除key</span>
        connection<span class="token punctuation">.</span><span class="token function">del</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token string">&quot;key2&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//随机返回key</span>
        connection<span class="token punctuation">.</span><span class="token function">randomKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//数据库可以大小</span>
        connection<span class="token punctuation">.</span><span class="token function">dbSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//按规则匹配key</span>
        connection<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>pattern<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//增量式迭代，匹配返回key，大数据集合会阻塞redis（单线程）</span>
        connection<span class="token punctuation">.</span><span class="token function">scan</span><span class="token punctuation">(</span><span class="token class-name">ScanOptions</span><span class="token punctuation">.</span><span class="token function">scanOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">&quot;*&quot;</span><span class="token comment">/*pattern*/</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//迭代集合键中的元素。</span>
        connection<span class="token punctuation">.</span><span class="token function">zScan</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">ScanOptions</span><span class="token punctuation">.</span><span class="token function">scanOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">&quot;*&quot;</span><span class="token comment">/*pattern*/</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//迭代有序集合中的元素（包括元素成员和元素分值）。</span>
        connection<span class="token punctuation">.</span><span class="token function">sScan</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">ScanOptions</span><span class="token punctuation">.</span><span class="token function">scanOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">&quot;*&quot;</span><span class="token comment">/*pattern*/</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//迭代哈希键中的键值对。</span>
        connection<span class="token punctuation">.</span><span class="token function">hScan</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">ScanOptions</span><span class="token punctuation">.</span><span class="token function">scanOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">&quot;*&quot;</span><span class="token comment">/*pattern*/</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//list数据结构排序，默认以为数字排序的列表</span>
        connection<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">SortParameters</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">getOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token class-name">Boolean</span> <span class="token function">isAlphabetic</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">//true 按字符排序</span>
                <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getByPattern</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getGetPattern</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token class-name">Range</span> <span class="token function">getLimit</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//清除当前数据库key</span>
        connection<span class="token punctuation">.</span><span class="token function">flushDb</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">flushAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//清除redis中所有key</span>
        <span class="token comment">//切换数据库</span>
        connection<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span>dbIndex<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br></div></div><h3 id="自动过期"><a href="#自动过期" class="header-anchor">#</a> 自动过期</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Long</span> secondTimeout <span class="token operator">=</span> <span class="token number">10l</span><span class="token punctuation">;</span>
        <span class="token class-name">Calendar</span> calendar <span class="token operator">=</span> <span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        calendar<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span>SECOND<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">long</span> timeout <span class="token operator">=</span> calendar<span class="token punctuation">.</span><span class="token function">getTimeInMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//设置过期，单位秒</span>
        connection<span class="token punctuation">.</span><span class="token function">expire</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> secondTimeout<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">expireAt</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> timeout<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//时间戳</span>
        <span class="token comment">//以秒单位返回</span>
        connection<span class="token punctuation">.</span><span class="token function">ttl</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">ttl</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span>HOURS<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//转化单位</span>
        <span class="token comment">//以毫秒单位返回</span>
        connection<span class="token punctuation">.</span><span class="token function">pTtl</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">pTtl</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span>SECONDS<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//转化单位</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="事务"><a href="#事务" class="header-anchor">#</a> 事务</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>redisTemplate<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RedisCallback</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">doInRedis</span><span class="token punctuation">(</span><span class="token class-name">RedisConnection</span> connection<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">DataAccessException</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> keyBytes <span class="token operator">=</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> valueBytes <span class="token operator">=</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//标记事务开始</span>
        connection<span class="token punctuation">.</span><span class="token function">multi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> valueBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//事务执行，multi命令后到现在中间的所有命令</span>
        connection<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//事务监听key，若可以在事务命令中，被其他客户端修改，事务失败</span>
        connection<span class="token punctuation">.</span><span class="token function">watch</span><span class="token punctuation">(</span>keyBytes<span class="token punctuation">,</span> keyBytes<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//取消事务监听</span>
        connection<span class="token punctuation">.</span><span class="token function">unwatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="现象-策略"><a href="#现象-策略" class="header-anchor">#</a> 现象&amp;策略</h2> <h3 id="缓存过期-缓存淘汰"><a href="#缓存过期-缓存淘汰" class="header-anchor">#</a> 缓存过期 &amp; 缓存淘汰</h3> <ul><li>定期删除：每100ms删除一次，随机算法删除部分过期缓存</li> <li>惰性删除：随机算法没碰到的数据，已过期，被查询就触发删除</li> <li>内存淘汰策略：以上两种都没删除过期缓存，导致内存不足的话，以下策略提供程序设置
<ul><li><strong>noeviction</strong>：返回错误，不会删除任何键值</li> <li><strong>allkeys-lru</strong>：使用LRU算法删除最近最少使用的键值</li> <li><strong>volatile-lru</strong>：使用LRU算法从设置了过期时间的键集合中删除最近最少使用的键值</li> <li><strong>allkeys-random</strong>：从所有key随机删除</li> <li><strong>volatile-random</strong>：从设置了过期时间的键的集合中随机删除</li> <li><strong>volatile-ttl</strong>：从设置了过期时间的键中删除剩余时间最短的键</li> <li><strong>volatile-lfu</strong>：从配置了过期时间的键中删除使用频率最少的键</li> <li><strong>allkeys-lfu</strong>：从所有键中删除使用频率最少的键</li></ul></li></ul> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201012102248.png" alt=""></p> <h3 id="缓存穿透-布隆过滤器"><a href="#缓存穿透-布隆过滤器" class="header-anchor">#</a> 缓存穿透 &amp; 布隆过滤器</h3> <ul><li><p>穿透：即不存在的数据，数据库中没有，Redis也无法缓存，浪费资源的无效请求查询</p></li> <li><p>策略：<a href="https://mp.weixin.qq.com/s?__biz=MzI1MzYzMTI2Ng==&amp;mid=2247483840&amp;idx=1&amp;sn=351b3c648a724b28bce767d9345400dd&amp;scene=21#wechat_redirect" target="_blank" rel="noopener noreferrer">《白话布隆过滤器BloomFilter》<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，能快速从超大数据集中得知数据是否存在，不存在就不用耗费资源查询</p></li></ul> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201012103608.png" alt=""></p> <h3 id="缓存击穿-缓存雪崩"><a href="#缓存击穿-缓存雪崩" class="header-anchor">#</a> 缓存击穿 &amp; 缓存雪崩</h3> <ul><li><p>击穿：即某次请求的数据缓存，刚好过期删除了，导致又去数据库查询</p></li> <li><p>雪崩：大批请求，都发生击穿现象，几乎同时，规模更大</p></li> <li><p>策略：缓存数据的过期设置需要均匀些，不要让大量数据集体失效</p></li></ul> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201012103210.png" alt=""></p> <h3 id="进程崩溃-持久化"><a href="#进程崩溃-持久化" class="header-anchor">#</a> 进程崩溃 &amp; 持久化</h3> <p>即备份缓存，进程崩溃，重启，还原缓存数据，策略如下</p> <ul><li><p>RDB持久化：二进制格式，配置参数来周期性备份，由子进程操作，不适合短周期，如分钟级别的变更</p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201012110808.png" alt=""></p></li> <li><p>AOF持久化：短周期备份方案，参考于<strong>MySQL:binlog</strong></p> <ul><li>临时缓冲区：缓存写入命令</li> <li>文件重写：压缩文件大小</li> <li>重写缓冲区：保证重写时新的写入命令遗漏补充</li></ul> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201012114352.png" alt=""></p></li></ul> <h2 id="集群"><a href="#集群" class="header-anchor">#</a> 集群</h2> <blockquote><ul><li><p>参考文档：<a href="http://redisdoc.com/topic/cluster-tutorial.html" target="_blank" rel="noopener noreferrer">官方文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></li> <li></li> <li><p>实际情况应该是多台服务器，一台一个Redis（主+从）来搭建</p></li> <li><p>配置文件<code>redis.cnf</code>：<a href="http://download.redis.io/redis-stable/redis.conf" target="_blank" rel="noopener noreferrer">官方包参考文件<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></li> <li><p>配置文件中集群相关属性</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 端口</span>
port <span class="token number">6379</span>
<span class="token comment"># 密码</span>
requirepass <span class="token number">123456</span>
<span class="token comment"># IP，默认本地访问，改为0.0.0.0支持所有主机访问</span>
bind <span class="token number">0.0</span><span class="token number">.0</span><span class="token number">.0</span>
<span class="token comment"># 启动集群模式</span>
cluster<span class="token operator">-</span>enabled yes
<span class="token comment"># 节点配置文件，默认生成/data/nodes.conf</span>
cluster<span class="token operator">-</span>config<span class="token operator">-</span><span class="token builtin">file</span> nodes<span class="token punctuation">.</span>conf
<span class="token comment"># 节点连接超时</span>
cluster<span class="token operator">-</span>node<span class="token operator">-</span>timeout <span class="token number">5000</span>
<span class="token comment"># AOF持久化，可选开启</span>
appendonly yes
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></li> <li><p>集群节点，至少3个实例，即3主，推荐3主3从，6个实例</p></li> <li><p>读取指定配置文件启动：<code>./redis-server ./redis.conf</code></p></li> <li><p>集群创建程序：<strong>redis-trib.rb或redis-cli</strong></p></li></ul></blockquote> <h3 id="docker搭建"><a href="#docker搭建" class="header-anchor">#</a> Docker搭建</h3> <blockquote><p>目录结构参考，根目录为redis，所有生成docker网络为<strong>redis-default</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>├─redis-single---------------------单一服务，不参与集群，测试用
│  ├─redis-single.yaml-------------单一服务的docker-compose
│  ├─conf
│  ├─data
│  ├─slave-1-----------------------集群节点，目录结构，其他一样
│  │  ├─conf
│  │  │	 ├─redis.cnf---------------节点配置文件
│  │  ├─data
│  ├─slave-2
│  ├─slave-3
│  ├─slave-4
│  ├─slave-5
│  ├─slave-6
│  ├─redis-cluster.yaml------------集群节点的docker-compose
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></blockquote> <ul><li><p>单一服务（single），不参与集群搭建，方面后续测试</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">&quot;3&quot;</span>

<span class="token comment"># 和集群存在同一网络中</span>
<span class="token key atrule">networks</span><span class="token punctuation">:</span>
  <span class="token key atrule">mynet</span><span class="token punctuation">:</span>
    <span class="token key atrule">external</span><span class="token punctuation">:</span>
      <span class="token key atrule">name</span><span class="token punctuation">:</span> redis_default

<span class="token key atrule">services</span><span class="token punctuation">:</span>
  <span class="token key atrule">redis</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>single
    <span class="token comment"># 等效于自定义Dockerfile中的CMD [ &quot;redis-server&quot;, &quot;/usr/local/etc/redis/redis.conf&quot; ]</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7010<span class="token punctuation">:</span><span class="token number">6379</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> /yh/redis/single/data<span class="token punctuation">:</span>/data
      <span class="token punctuation">-</span> /yh/redis/single/conf/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf
    <span class="token key atrule">networks</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> mynet
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div></li> <li><p>集群节点服务</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">&quot;3.4&quot;</span>

<span class="token key atrule">services</span><span class="token punctuation">:</span>
  <span class="token key atrule">redisS1</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>slave<span class="token punctuation">-</span><span class="token number">1</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7011<span class="token punctuation">:</span><span class="token number">7011</span>
      <span class="token punctuation">-</span> 17011<span class="token punctuation">:</span><span class="token number">17011</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>1/data<span class="token punctuation">:</span>/data
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>1/conf/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf
      
  <span class="token key atrule">redisS2</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>slave<span class="token punctuation">-</span><span class="token number">2</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7012<span class="token punctuation">:</span><span class="token number">7012</span>
      <span class="token punctuation">-</span> 17012<span class="token punctuation">:</span><span class="token number">17012</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>2/data<span class="token punctuation">:</span>/data
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>2/conf/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf
      
  <span class="token key atrule">redisS3</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>slave<span class="token punctuation">-</span><span class="token number">3</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7013<span class="token punctuation">:</span><span class="token number">7013</span>
      <span class="token punctuation">-</span> 17013<span class="token punctuation">:</span><span class="token number">17013</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>3/data<span class="token punctuation">:</span>/data
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>3/conf/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf
      
  <span class="token key atrule">redisS4</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>slave<span class="token punctuation">-</span><span class="token number">4</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7014<span class="token punctuation">:</span><span class="token number">7014</span>
      <span class="token punctuation">-</span> 17014<span class="token punctuation">:</span><span class="token number">17014</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>4/data<span class="token punctuation">:</span>/data
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>4/conf/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf
      
  <span class="token key atrule">redisS5</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>slave<span class="token punctuation">-</span><span class="token number">5</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7015<span class="token punctuation">:</span><span class="token number">7015</span>
      <span class="token punctuation">-</span> 17015<span class="token punctuation">:</span><span class="token number">17015</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>5/data<span class="token punctuation">:</span>/data
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>5/conf/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf
      
  <span class="token key atrule">redisS6</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis
    <span class="token key atrule">container_name</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>slave<span class="token punctuation">-</span><span class="token number">6</span>
    <span class="token key atrule">command</span><span class="token punctuation">:</span> redis<span class="token punctuation">-</span>server /usr/local/etc/redis/redis.conf
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> 7016<span class="token punctuation">:</span><span class="token number">7016</span>
      <span class="token punctuation">-</span> 17016<span class="token punctuation">:</span><span class="token number">17016</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>6/data<span class="token punctuation">:</span>/data
      <span class="token punctuation">-</span> /yh/redis/slave<span class="token punctuation">-</span>6/conf/redis.conf<span class="token punctuation">:</span>/usr/local/etc/redis/redis.conf
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br></div></div></li> <li><p>启动服务</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 启动</span>
docker-compose -f redis-single/redis-single.yaml up -d
docker-compose -f redis-cluster.yaml up -d
<span class="token comment"># 查看</span>
docker <span class="token function">ps</span> -a
<span class="token comment"># 查看该网络下的的地址IP，主要看各节点服务</span>
docker network insepect redis-default
<span class="token comment">## 比如 172.25.0.4:7011</span>
<span class="token comment">## 比如 172.25.0.3:7012</span>
<span class="token comment">## 比如 172.25.0.6:7013</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li></ul> <h3 id="集群操作"><a href="#集群操作" class="header-anchor">#</a> 集群操作</h3> <blockquote><p>以docker搭建环境，模拟多台服务器操作</p></blockquote> <ul><li><p>创建集群</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 用single服务测试，进入容器内</span>
docker <span class="token builtin class-name">exec</span> -it redis-single <span class="token function">bash</span>
<span class="token comment"># 创建集群命令，[a]表示密码，IP端口为上述查询到节点容器地址</span>
redis-cli -a <span class="token number">123456</span> --cluster create <span class="token number">172.25</span>.0.6:7011 <span class="token number">172.25</span>.0.5:7012 <span class="token number">172.25</span>.0.4:7013 --cluster-replicas <span class="token number">0</span>
<span class="token comment"># 日志删除提示，输入yes</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><blockquote><p>replicas：主从比例，至少3个实例构建集群，即0表示1主0从，1表示1主1从，至少6个服务，依次类推</p></blockquote></li> <li><p>测试</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## single服务容器内部 </span>
<span class="token comment"># 显示所有节点，地址只要集群任意节点都行</span>
redis-cli -a <span class="token number">123456</span> -h <span class="token number">172.25</span>.0.6 -p <span class="token number">7011</span> cluster nodes
<span class="token comment"># 显示集群信息</span>
redis-cli -a <span class="token number">123456</span> -h <span class="token number">172.25</span>.0.6 -p <span class="token number">7011</span> cluster info
<span class="token comment"># 显示节点信息</span>
redis-cli --cluster info <span class="token number">172.25</span>.0.6:7011 -a <span class="token number">123456</span>
redis-cli --cluster check <span class="token number">172.25</span>.0.6:7011 -a <span class="token number">123456</span>
<span class="token comment"># 以集群模式连接redis，注意-c参数</span>
redis-cli -a <span class="token number">123456</span> -h <span class="token number">172.25</span>.0.6 -p <span class="token number">7011</span> -c
<span class="token comment">## 以上命令都可以直接进入到redis-cli内部，运行查询</span>
<span class="token comment">## 进入redis-cli内部，显示	-&gt;172.25.0.6:7011&gt;</span>
<span class="token comment"># 存储，随机存放集群中某个节点，假设分布在节点A、B、C</span>
<span class="token builtin class-name">set</span> a a
<span class="token builtin class-name">set</span> b b
<span class="token builtin class-name">set</span> c c
<span class="token comment"># 获取数据，自动根据hash算出转发到某个节点再获取</span>
get a 
get b
get c
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><blockquote><ul><li>这里的集群配置cluster-replicas为0，即3主0从，某个节点服务停了即该节点数据就没了</li> <li>集群推荐主从配置，即至少6个，A主服务挂了，A1从服务变为主服务，重启A变为从服务，自动切换！</li></ul></blockquote></li> <li><p>新增节点服务、删除</p> <ul><li><p>新增主节点</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## single服务容器内部</span>
<span class="token comment"># 新增7014节点，加到集群中（写集群中任意节点都行，此处为7011）</span>
redis-cli --cluster add-node <span class="token number">172.25</span>.0.3:7014 <span class="token number">172.25</span>.0.6:7011
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li> <li><p>新增从节点</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## single服务容器内部</span>
<span class="token comment"># 显示所有节点，查看现在节点的ID</span>
redis-cli -a <span class="token number">123456</span> -h <span class="token number">172.25</span>.0.6 -p <span class="token number">7011</span> cluster nodes
<span class="token comment"># 新增7015节点，加为7011节点的从节点</span>
redis-cli --cluster add-node <span class="token number">172.25</span>.0.2:7015 <span class="token number">172.25</span>.0.6:7011 --cluster-slave --cluster-master-id c30decc4ec6520a1adb2888e05e0de8ef19d311c -a <span class="token number">123456</span>
<span class="token comment"># 同理，把7016也加上，加为7014节点的从节点</span>
<span class="token comment"># 可再查看下所有节点，即已经添加</span>

<span class="token comment">## 进入主从节点reids-cli下</span>
<span class="token comment"># 查看对应连接的主从节点</span>
info replication
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></li> <li><p>删除节点</p> <blockquote><p>必须先清除槽数量，即重新分片；</p></blockquote> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## single服务容器内部</span>
redis-cli --cluster del-node <span class="token variable"><span class="token variable">`</span><span class="token operator">&lt;</span>IP:PORT<span class="token operator">&gt;</span><span class="token variable">`</span></span> <span class="token variable"><span class="token variable">`</span><span class="token operator">&lt;</span>node-id<span class="token operator">&gt;</span><span class="token variable">`</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul></li> <li><p>集群重新分片</p> <ul><li>集群内置了<strong>16384</strong>个哈希槽，每个key通过CRC16校验后对应哈希槽中一个值</li> <li>哈希槽默认平均分配给每个节点服务</li> <li>有利于节点变动（新增、删除）重新分配哈希槽</li></ul> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 查看所有节点分配情况</span>
redis-cli -a <span class="token number">123456</span> -h <span class="token number">172.25</span>.0.6 -p <span class="token number">7011</span> cluster nodes
<span class="token comment"># 显示结果，每行最后显示当前分片范围，如</span>
<span class="token comment">##  172.25.0.4:7013@17013 master 。。。。。。 10923-16383</span>
<span class="token comment">## 	172.25.0.5:7012@17012 myself,master 。。。。。。。 5461-10922</span>
<span class="token comment">##	172.25.0.6:7011@17011 master 。。。。。。。。d 0-5460</span>
<span class="token comment"># 即存储的key对应值为几，就存在哪个节点服务</span>

<span class="token comment"># 集群重新分片（任意集群几点）</span>
redis-cli --cluster reshard <span class="token number">172.25</span>.0.3:7014 -a <span class="token number">123456</span>
<span class="token comment">## 根据提示，输入要分片的数量</span>
<span class="token comment">## 根据提示，输入要接收的节点ID</span>
<span class="token comment">## 根据提示，选择分片来源，all或节点ID，多个逗号区分</span>

<span class="token comment"># 自动平衡分片</span>
redis-cli --cluster rebalance --cluster-threshold <span class="token number">1</span> --cluster-use-empty-masters <span class="token number">172.25</span>.0.6:7011 -a <span class="token number">123456</span>
<span class="token comment"># 再通过cluster info命令查看已平均每个主节点的哈希槽</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></li></ul> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201027142511.png" alt=""></p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20201027142736.png" alt=""></p> <blockquote><p>问题：</p> <ol><li><p>主从不同步</p></li> <li><p>主挂了，从节点没有自动升为主节点</p></li></ol></blockquote></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/5/27 上午11:15:53</span></div></footer> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/blog/assets/js/app.0c46c7d8.js" defer></script><script src="/blog/assets/js/3.5679fe00.js" defer></script><script src="/blog/assets/js/1.8cdd2163.js" defer></script><script src="/blog/assets/js/19.e0e5e91a.js" defer></script>
  </body>
</html>
